/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package codegener.tool;

import codegener.entity.ColumnInfo;
import codegener.entity.TableInfo;
import java.util.ArrayList;

import java.util.List;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;


/**
 * 达梦表信息获取
 * @author 32181
 */
public class DMTabGet {
    
    public List<String> listTables(){
        try{
           return DBGet.getQR().query(DBGet.getConn(),"SELECT TABLE_NAME FROM USER_TABLES", new ColumnListHandler<String>());
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    public List<String> listTables(String likeName){
        try{
           return DBGet.getQR().query(DBGet.getConn(),"SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME LIKE '%"+likeName+"%'", new ColumnListHandler<String>());
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
     public List<TableInfo> tableDat(String tabName){
        try{
            if(null==tabName){
                return DBGet.getQR().query(DBGet.getConn(),"select table_name name,num_rows numRows from user_tables ORDER BY NUM_ROWS DESC", new BeanListHandler<TableInfo>(TableInfo.class));
            }else{
               return DBGet.getQR().query(DBGet.getConn(),"select table_name name,num_rows numRows from user_tables  WHERE TABLE_NAME = '"+tabName+"'", new BeanListHandler<TableInfo>(TableInfo.class));     
            }
           
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    public List<TableInfo> getTable(String... tableNames)throws Exception{
        if(null==tableNames||tableNames.length==0){
            return null;
        }
        List<TableInfo> rsInfos=new ArrayList<>();
        for(String tableName:tableNames){
            TableInfo tableInfo=new TableInfo();
            List<ColumnInfo> columns=DBGet.getQR().query(DBGet.getConn(),"select t.table_name tableName,t.column_name name,t.column_name cname,t.data_type dataType,t.data_length maxLength,t.nullable is_null,c.comments ,t.column_id order_num,uc.comments tabcomment"
                                                       + " from user_tab_columns t left join user_col_comments c on t.table_name = c.table_name and t.column_name = c.column_name "
                                                       +"  LEFT JOIN user_tab_comments uc on t.table_name=uc.table_name "
                                                       + " where t.table_name='"+tableName+"' order by t.column_id", new BeanListHandler<ColumnInfo>(ColumnInfo.class));
            if(null!=columns&&!columns.isEmpty()){
                tableInfo.setName(tableName);
                tableInfo.setColumns(columns);
                tableInfo.setTabcomment(columns.get(0).getTabcomment());
            }

            List<String> priColumns=DBGet.getQR().query(DBGet.getConn(),"select col.column_name from user_constraints con,user_cons_columns col where con.constraint_name=col.constraint_name and con.constraint_type='P' and col.table_name='"+tableName+"'", new ColumnListHandler<String>());

            if(null!=priColumns&&!priColumns.isEmpty()){
                for(String priColumn:priColumns){
                    for(ColumnInfo column:columns){
                        if(column.getCname().equals(priColumn)){
                            column.setIsPrimary(priColumn);
                            column.setPrimary(true);
                        }
                        if("Y".equalsIgnoreCase(column.getIsNull())){
                            column.setNullAble(true);
                        }
                    }
                }            
            }
            rsInfos.add(tableInfo);
        }
        
        return rsInfos;
    }
    
}
